<template>
  <div style="text-align: center">
    <h2>你好！{{ userInfo.username }}</h2>

    <el-form
      :model="passForm"
      status-icon
      :rules="rules"
      ref="passForm"
      label-width="100px"
    >
      <el-upload
        ref="avatarImg"
        class="avatar-uploader"
        action="#"
        :show-file-list="false"
        :on-change="handleChange"
        :http-request="uploadImg"
      >
        <img v-if="imageUrl" :src="imageUrl" class="avatar" />
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
      <el-form-item label="邮箱" prop="passForm.email">
        <el-input v-model="passForm.email"></el-input>
      </el-form-item>
      <el-form-item label="电话号码" prop="passForm.phone">
        <el-input v-model="passForm.phone"></el-input>
      </el-form-item>
      <el-form-item label="旧密码" prop="currentPass">
        <el-input
          type="password"
          v-model="passForm.currentPass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="password">
        <el-input
          type="password"
          v-model="passForm.password"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input
          type="password"
          v-model="passForm.checkPass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('passForm')"
          >提交</el-button
        >
        <el-button @click="resetForm('passForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.passForm.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      userInfo: {},
      imageUrl: "",
      tempUrl: "",
      passForm: {
        email: "",
        phone: "",
        password: "",
        checkPass: "",
        currentPass: "",
      },
      rules: {
        password: [
          { required: true, message: "请输入新密码", trigger: "blur" },
          {
            min: 6,
            max: 12,
            message: "长度在 6 到 12 个字符",
            trigger: "blur",
          },
        ],
        checkPass: [
          { required: true, validator: validatePass, trigger: "blur" },
        ],
        currentPass: [
          { required: true, message: "请输入当前密码", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getUserInfo();
  },
  methods: {
    getUserInfo() {
      this.axios.get("/sys/userInfo").then((res) => {
        this.userInfo = res.data.data;
        this.imageUrl = this.userInfo.avatar;
        this.passForm.email = this.userInfo.email;
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const _this = this;
          this.axios.post("/sys/user/updatePass", this.passForm).then((res) => {
            _this.$alert(res.data.msg, "提示", {
              confirmButtonText: "确定",
              callback: (action) => {
                this.$refs[formName].resetFields();
              },
            });
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleChange(file, fileList){
      this.tempUrl = URL.createObjectURL(file.raw)
    },
    uploadImg(item) {
      let formdata = new FormData();
      formdata.append('authorization', localStorage.getItem('token'))
      formdata.append("image", item.file);
      this.uploadFileRequest(
        "http://localhost:8082/upload",
        formdata
      ).then((res) => {
        console.log(res.data)
        this.imageUrl = res.data
      });
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = res.data;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

<style scoped>
.el-form {
  width: 420px;
  margin: 50px auto;
}
</style>

<style>
.el-form {
  width: 420px;
  margin: 50px auto;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>